<script setup>
import { ref } from 'vue'
import { api } from '../../js/ajax'
import { tools } from '../../js/tools'
import { ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()

const queryInfo = ref({
  title: '',
  order: 1,
})
const page = ref({
  pageNumber: 1,
  pageSize: 12,
})
const list = ref([])

const showDetail = nid => {
  router.push({
    path: '/crud/newsdetail',
    query: { nid: nid },
  })
}

const query = () => {
  api.get('/crud/news', tools.mergeJson(queryInfo.value, page.value), data => {
    if (data.success) {
      list.value = data.data.data
      page.value = data.data.pageBean
      return
    }
    ElMessageBox.alert(data.message, '新闻管理')
  })
}

query()
</script>

<template>
  <div class="news">
    <div v-for="d in list" :key="d.nid">
      <el-card>
        <template #header>
          <div class="card-header">
            <span>{{ d.source }}</span>
          </div>
        </template>
        <div @click="showDetail(d.nid)" class="news-title">
          {{ d.title }}
        </div>
        <template #footer>
          <div class="card-footer">
            {{ tools.formatDate(d.lastupdate) }}
          </div>
        </template>
      </el-card>
    </div>
    <!-- {{ list }} -->
  </div>
</template>

<style scoped>
.card-header,
.card-footer {
  color: #999;
  font-size: 0.8em;
}

.card-footer {
  text-align: right;
}

.news {
  display: flex;
  flex-wrap: wrap;
}

.news > div {
  margin: 1rem;
  width: calc(33vw - 3rem);
}

.news-title {
  cursor: pointer;
  font-size: 1.2em;
  text-align: center;
  height: 3em;
}
</style>
